package com.android.nearshop.app.activity.setting;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.TemplateActivity;
import com.android.nearshop.app.activity.main.MainIndexActivity;
import com.android.nearshop.app.adapter.listadapter.SearchHistoryAdapter;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.app.widget.TitleBar.OnTitleClickListener;
import com.android.nearshop.business.update.UpdateActivity;
import com.android.nearshop.io.database.ShopDB;
import com.android.nearshop.io.protocol.ProtocolManager;
import com.android.nearshop.io.protocol.in.AddSuggestionAction;
import com.android.nearshop.io.protocol.in.SoapAction.ActionListener;
import com.android.nearshop.util.Constant;

/**
 * 设置功能页面
 * @author huangyue
 *
 */
public class SettingFunctionActivity extends TemplateActivity implements OnClickListener{
	public static final int ABOUT_US = 1;
	public static final int REPORT = 2;
	public static final int LAST_SEE = 3;
	public static final int SERVICE = 4;
	public static final int HELP = 5;
	
	private int type;
	
	private SearchHistoryAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		type = intent.getIntExtra(Constant.TAG_SETTING_FUNCTION, -1);
		
		switch (type) {
		case ABOUT_US:
		{
			setContentView(R.layout.activity_about_us);
			try {
				PackageManager pm = getPackageManager();  
				PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
				TextView version = (TextView)findViewById(R.id.version_code);
		        TextView text = (TextView) findViewById(R.id.version_txt);
				text.setText(getString(R.string.version_info_title, pi.versionName));
				mTitleBar.setTitle(R.string.about_us);
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			
			break;
		}
		case REPORT:
		{
			setContentView(R.layout.activity_settings_report);
			findViewById(R.id.report_send).setOnClickListener(this);
			mTitleBar.setTitle(R.string.setting_report);
			break;
		}
		case LAST_SEE:
		{
			setContentView(R.layout.activity_settings_last_review);
			ListView listView = (ListView) findViewById(R.id.listView);
			adapter = new SearchHistoryAdapter(this);
			listView.setAdapter(adapter);
			mTitleBar.setTitle(R.string.setting_lastsee);
			adapter.loadData();
			break;
		}
		case SERVICE:
		{
			setContentView(R.layout.activity_protool);
			mTitleBar.setTitle(R.string.setting_protocol);
			TextView text =  (TextView) findViewById(R.id.protool_txt);
			InputStream is = null;
			try {
				is = getResources().getAssets().open("service.txt");
				byte[] bytes = new byte[is.available()];
				is.read(bytes);
				text.setText(new String(bytes));
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if(is != null){
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			break;
		}
		case HELP:
		{
			WebView webView = new WebView(this);
			webView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			webView.loadUrl("file:///android_asset/help.html");
			setContentView(webView);
			mTitleBar.setTitle(R.string.setting_help);
			break;
		}
		default:
			break;
		}
	}
	
	@Override
	protected void setupTitleBar(TitleBar titleBar) {
		if(type != LAST_SEE){
			titleBar.setRightVisible(false);
		} else {
			titleBar.setRigthImage(0);
			titleBar.setRigthText(R.string.clear_all);
		}
		
		titleBar.setOnTitleClickListener(new OnTitleClickListener()
        {       
			@Override
			public void onLeftClick() {
				finish();
			}
			
            @Override
            public void onRightClick()
            {
                ShopDB.clear(ShopDB.TABLE_NAME, null);
                adapter.loadData();
                Toast.makeText(SettingFunctionActivity.this, R.string.select_setting_clear_text, Toast.LENGTH_LONG).show();
            }
        },  true, true);
		
	}
	
    public static void startActivity(Activity context, int type)
    {
        Intent intent = new Intent(context, SettingFunctionActivity.class);
        intent.putExtra(Constant.TAG_SETTING_FUNCTION, type);
        context.startActivity(intent);
    }

	@Override
	public void onClick(View v) 
	{
	    switch(v.getId())
	    {
    	    case R.id.report_send:
    	    {
    	    	EditText name = (EditText) findViewById(R.id.report_name);
    	    	EditText phone = (EditText) findViewById(R.id.report_phone);
    	    	EditText send = (EditText) findViewById(R.id.report_text);
    	    	final ProgressDialog progressDialog = ProgressDialog.show(SettingFunctionActivity.this, "Sending...", "Please wait...", true, false); 
    	    	
    	    	AddSuggestionAction mAction = new AddSuggestionAction(null, name.getText()!=null ? name.getText().toString() : null, phone.getText()!=null ? phone.getText().toString() : null,
    	    			send.getText()!=null ? send.getText().toString() : null);
    	    	mAction.setActionListener(new ActionListener<Integer>(){
					@Override
					public void onSucceed(Integer result) {
						progressDialog.dismiss();
						if(result != null && result == 1){
							showToast(getString(R.string.send_seccus));
						}else{
							showToast(getString(R.string.send_failed));
						}
					}
					@Override
					public void onError(int resultCode) {
						progressDialog.dismiss();
						showToast(getString(R.string.send_failed));
					}
    	    	});
    	    	ProtocolManager.getProtocolManager().submitAction(mAction);
    	        break;
    	    }
	    }
	}
}
